﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcDDshop2.Controllers
{
    public class CartController : Controller
    {
        //
        // GET: /Cart/

        //显示购物车
        public ActionResult Cart()
        {
            if (Session["userid"] == null)
            {
                return RedirectToAction("Login", "Login");
            }
            else
            {
                //获取session中的数据
                List<Car> carlist = Session["car"] as List<Car>;
                //判断session是否为空
                if (carlist == null)
                {
                    //为空的话需要实例化
                    carlist = new List<Car>();
                }
                //讲数据存至viewbag中 等待视图调用
                ViewBag.list = carlist;
                decimal money = 0;
                foreach (Car c in carlist)
                {
                    money += c.price;
                }
                ViewBag.money = money;
                return View();
            }
        }

        //购物车功能
        public ActionResult Car(int id)
        {
            if (Session["userid"] == null)
            {
                return RedirectToAction("Login", "Login");
            }
            else
            {
                Book book = BLL.Bll.book_id(id);
                List<Car> listcar = Session["car"] as List<Car>;
                if (listcar == null)
                {
                    listcar = new List<Car>();
                    Car c = new Car();
                    c.ID = book.ID;
                    c.price = book.Price;
                    c.bookname = book.bookname;
                    c.Pic = book.Pic;
                    listcar.Add(c);
                }
                else
                {
                    bool a = true;
                    foreach (Car c in listcar)
                    {
                        if (c.ID == book.ID)
                        {
                            a = false;
                        }
                    }
                    if (a)
                    {
                        Car cc = new Car();
                        cc.ID = book.ID;
                        cc.price = book.Price;
                        cc.bookname = book.bookname;
                        cc.Pic = book.Pic;
                        listcar.Add(cc);
                    }
                }
                Session["car"] = listcar;
                return RedirectToAction("Detail", "Detail", new { id = book.ID });
            }
            //创建一个guid
            //string gui = Convert.ToString(System.Guid.NewGuid());
            //ViewBag.id = gui;
            //return View();
        }

        //购物车删除功能
        public ActionResult delete(int id)
        {
            //获取session中的数据
            List<Car> carlist = Session["car"] as List<Car>;
            //获取需要删除的数据 用对象来存储
            Car c = carlist.Where(a => a.ID == id).FirstOrDefault();
            //删除
            carlist.Remove(c);
            //将删除后的结果 再存储到session中
            Session["car"] = carlist;
            return RedirectToAction("Cart", "Cart");
        }
    }
}
